package com.example.main.yyyymmdd;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main02 {

    private static final Pattern patternBody = Pattern.compile("\\{([^}]*)\\}");
    private static final String ANY_PATTERN=".*";

    public static void main(String[] args) {
        String file="a{yyyyMMdd}fd-{yyyyMMdd(-1)}er{yyyyMMdd(2)}}.csv";
        String s = parseBody(file);
        System.out.println();
    }

    public static String parseBody(String body) {
        String text = body;
        Matcher m = patternBody.matcher(text);
        while (m.find()){
            String parse = m.group();
            String parseBody = parse.substring(1).substring(0, parse.length() - 2);

            // 1. 设置显示格式
            String showFormat = parseBody;
            Long change=0L;
            // 日期显示格式 去掉小括号后的内容
            int left = parseBody.indexOf("(");
            int right = parseBody.indexOf(")");
            if (left >= 0) {
                showFormat = showFormat.substring(0, left);
                change=Long.valueOf(parseBody.substring(left+1,right));
            }

            String dateStr = LocalDate.now().plusDays(change).format(DateTimeFormatter.ofPattern(showFormat));
            text = text.replace(parse, dateStr);
        }

        return text;
    }
}
